package it.univaq.mwt.business.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;






import it.univaq.mwt.business.MessageService;
import it.univaq.mwt.business.model.Conversation;
import it.univaq.mwt.business.model.Message;

@Stateless
@Remote(MessageService.class)
public class EJBMessages implements MessageService {

	@PersistenceContext(unitName="Yop-domain")
	private EntityManager em;
	
	
	@Resource
	private SessionContext etx;
	
	public EJBMessages() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<Message> findAllMessageByConversationId(int id) {
		
		Query query = em.createQuery("select ms "
				+ "from Message ms, Conversation cv "
				+ "where cv.id =:id");
		query.setParameter("id", id);
		List<Message> messages = new ArrayList<Message>(query.getResultList());
		return messages;
	}

	@Override
	//@Transactional
	public Message createMessage(Message messaggio) {
		
		Message msg = messaggio;
		System.out.println(msg.gettext());
		em.persist(msg);
		em.getEntityManagerFactory().getCache().evict(Conversation.class);
		
		return msg;
		
	}

}
